local jass = require 'jass.common'
local slk = require 'jass.slk'

--弹幕在屏幕的时间
local time = 10
local barrages = {}
local moves = {}
for a=1,30 do
    --创建ui控件
    barrages[a] = class.panel:builder 
    {
        x = 0,
        y = 0,
        w = 32,
        h = 32,
        normal_image = [[]], --图标

        text = {
            type = 'text',
            x = 38,
            w = 800,
            align = 4,
            font_size = 18,
        }
    }
end

ac.hardware:event '界面绘制'(function(_,frame)
    local speed = 1920 / time * frame
    local removes = {}

    for panel in pairs(moves) do
        local x = panel.x - speed
        --刷新位置
        panel:set_position(x, panel.y)
        if x + 300 < 0 then 
            panel:hide()
            table.insert(removes,panel)
        end 
    end
    for _,panel in ipairs(removes) do
        moves[panel] = nil
        table.insert(barrages,panel)
    end
end)


function barrage(player, text, art)
    local hero = player.hero
    local art = art or hero:get_art() or [[]]
    local text = player:get_color_name() .. ': '.. text
    local start = {
        x = 1920,
        y = math.random(50, 800), --魔兽随机数 切记不要异步调用
    }
    local panel = table.remove(barrages,1)
    if panel==nil then
        return
    end
    moves[panel] = true
    panel:show()
    panel:set_position(start.x,start.y)
    panel:set_normal_image(art)
    panel.text:set_text(text)

end

